package com.example.library.entity;

import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serial;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 借阅记录实体类
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("borrow_records")
public class BorrowRecord implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 借阅记录ID
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 用户ID
     */
    @TableField("user_id")
    private Long userId;

    /**
     * 图书ID
     */
    @TableField("book_id")
    private Long bookId;

    /**
     * 借阅日期
     */
    @TableField("borrow_date")
    private LocalDateTime borrowDate;

    /**
     * 应还日期
     */
    @TableField("due_date")
    private LocalDateTime dueDate;

    /**
     * 实际归还日期
     */
    @TableField("return_date")
    private LocalDateTime returnDate;

    /**
     * 状态：BORROWED-已借出，RETURNED-已归还，OVERDUE-逾期
     */
    @TableField("status")
    private String status;

    /**
     * 罚金金额
     */
    @TableField("fine_amount")
    private BigDecimal fineAmount;

    /**
     * 备注
     */
    @TableField("remarks")
    private String remarks;

    /**
     * 创建时间
     */
    @TableField(value = "created_time", fill = FieldFill.INSERT)
    private LocalDateTime createdTime;

    /**
     * 更新时间
     */
    @TableField(value = "updated_time", fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updatedTime;

    /**
     * 逻辑删除标识
     */
    @TableLogic
    @TableField("deleted")
    private Integer deleted;

    /**
     * 用户名（非数据库字段）
     */
    @TableField(exist = false)
    private String username;

    /**
     * 用户真实姓名（非数据库字段）
     */
    @TableField(exist = false)
    private String userRealName;

    /**
     * 图书标题（非数据库字段）
     */
    @TableField(exist = false)
    private String bookTitle;

    /**
     * 图书作者（非数据库字段）
     */
    @TableField(exist = false)
    private String bookAuthor;

    /**
     * 图书ISBN（非数据库字段）
     */
    @TableField(exist = false)
    private String bookIsbn;

    // 常量定义
    public static final String STATUS_BORROWED = "BORROWED";
    public static final String STATUS_RETURNED = "RETURNED";
    public static final String STATUS_OVERDUE = "OVERDUE";

    /**
     * 判断是否已借出
     */
    public boolean isBorrowed() {
        return STATUS_BORROWED.equals(this.status);
    }

    /**
     * 判断是否已归还
     */
    public boolean isReturned() {
        return STATUS_RETURNED.equals(this.status);
    }

    /**
     * 判断是否逾期
     */
    public boolean isOverdue() {
        return STATUS_OVERDUE.equals(this.status) ||
                (isBorrowed() && dueDate != null && LocalDateTime.now().isAfter(dueDate));
    }

    /**
     * 获取借阅天数
     */
    public long getBorrowDays() {
        if (borrowDate == null) {
            return 0;
        }
        LocalDateTime endDate = returnDate != null ? returnDate : LocalDateTime.now();
        return java.time.Duration.between(borrowDate, endDate).toDays();
    }

    /**
     * 获取逾期天数
     */
    public long getOverdueDays() {
        if (dueDate == null || !isOverdue()) {
            return 0;
        }
        LocalDateTime endDate = returnDate != null ? returnDate : LocalDateTime.now();
        return java.time.Duration.between(dueDate, endDate).toDays();
    }

    // 手动添加getter和setter方法
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public Long getBookId() {
        return bookId;
    }

    public void setBookId(Long bookId) {
        this.bookId = bookId;
    }

    public LocalDateTime getBorrowDate() {
        return borrowDate;
    }

    public void setBorrowDate(LocalDateTime borrowDate) {
        this.borrowDate = borrowDate;
    }

    public LocalDateTime getDueDate() {
        return dueDate;
    }

    public void setDueDate(LocalDateTime dueDate) {
        this.dueDate = dueDate;
    }

    public LocalDateTime getReturnDate() {
        return returnDate;
    }

    public void setReturnDate(LocalDateTime returnDate) {
        this.returnDate = returnDate;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public BigDecimal getFineAmount() {
        return fineAmount;
    }

    public void setFineAmount(BigDecimal fineAmount) {
        this.fineAmount = fineAmount;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

}